<template>
  <main-box>
    <PlusSearch
      v-model="queryform"
      :columns="formConfig"
      :show-number="2"
      label-width="80"
      label-position="right"
      class="mb-xs"
      @search="initData()"
      @reset="initData()"
    >
      <template #plus-field-warehouseId>
        <k-select-http
          v-model="queryform.warehouseId"
          paramsKey="searchValue"
          :immediately="true"
          :httpfn="getWarehouseList"
          :keyprops="{ label: 'name', value: 'warehouseId' }"
        />
      </template>
    </PlusSearch>

    <PureTable
      ref="tableRef"
      border
      adaptive
      :loading="loading"
      :data="tableData"
      :columns="columns"
      :pagination="pagination"
      @page-size-change="initData()"
      @page-current-change="initData(false)"
    />
  </main-box>
</template>

<script setup lang="tsx">
import { onMounted, ref } from "vue";
import { useTableConfig } from "@/utils/tableConfig";
import { type PlusColumn, PlusSearch } from "plus-pro-components";
import { useRouter } from "vue-router";
import { listApi } from "@/api/statistics/warn";
import kSelectHttp from "@/components/k-select-http/index.vue";
import { allListApi as getWarehouseList } from "@/api/warehouse";
import { useMultiTagsStoreHook } from "@/store/modules/multiTags";

const router = useRouter();

/* 查询表单 */
const formConfig: PlusColumn[] = [
  {
    label: "日期",
    prop: "time",
    valueType: "date-picker",
    fieldProps: {
      type: "daterange",
      valueFormat: "YYYY-MM-DD"
    }
  },
  { label: "工具室", prop: "warehouseId" }
];
const queryform = ref({ time: [], warehouseId: "" });

/* 表格数据 */
const { pagination } = useTableConfig();
const tableData = ref([]);
const loading = ref(false);
const columns = [
  { label: "日期", prop: "warnDateStr" },
  { label: "工具室", prop: "warehouseName" },
  { label: "报警次数", prop: "warnCount" },
  {
    label: "操作",
    prop: "action",
    cellRenderer: ({ row }) => {
      return (
        <el-link type="primary" underline={false} onClick={() => goRecord(row)}>
          报警记录
        </el-link>
      );
    }
  }
];
const initData = async (needInit = true) => {
  loading.value = false;
  if (needInit) {
    pagination.currentPage = 1;
  }
  const params = {
    pageNum: pagination.currentPage,
    pageSize: pagination.pageSize,
    warehouseId: queryform.value.warehouseId
  } as any;
  if (queryform.value.time && queryform.value.time.length) {
    params.params = {
      startTime: queryform.value.time[0],
      endTime: queryform.value.time[1]
    };
  }
  await listApi(params).then(res => {
    tableData.value = res.rows;
    pagination.total = Number(res.total);
  });
  loading.value = false;
};

onMounted(() => {
  initData();
});

const goRecord = ({ warehouseId, warnDateStr }) => {
  const query = {
    path: "/statistics/warn/record",
    query: { warehouseId, warnDateStr }
  };
  useMultiTagsStoreHook().handleTags("push", {
    ...query,
    meta: {
      title: "报警记录",
      dynamicLevel: 1
    }
  });
  router.push(query);
};
</script>
